
//
//  HomeTableViewCell.swift
//  CJPWB
//
//  Created by 陈锦彭 on 16/3/19.
//  Copyright © 2016年 chenjinpeng. All rights reserved.
//

import UIKit
import SDWebImage



class HomeTableViewCell: UITableViewCell {
  
    // MARK: - UI属性
    @IBOutlet weak var priflieImageView: UIImageView!
    
    @IBOutlet weak var verifiedImageView: UIImageView!
    
    @IBOutlet weak var screenNameLabel: UILabel!
    
    @IBOutlet weak var vipImageView: UIImageView!
    
    @IBOutlet weak var createAtLabel: UILabel!
    
    @IBOutlet weak var sourceStrLabel: UILabel!
    
    @IBOutlet weak var text_label: UILabel!
    
    @IBOutlet weak var toolBar: UIView!
    
    @IBOutlet weak var pictureCollectView: PictureCollectView!
    
    @IBOutlet weak var retweetedTextLabel: UILabel!
    
    // MARK: - 约束属性
    ///正文label的最大宽度
    @IBOutlet weak var textLabelWidthCons: NSLayoutConstraint!
    ///转发正文label的约束
    @IBOutlet weak var retweetedTextLabelWidthCons: NSLayoutConstraint!
    
    @IBOutlet weak var reweetTextLabelTopCons: NSLayoutConstraint!
 
    @IBOutlet weak var picViewTopCons: NSLayoutConstraint!
    // MARK: - 自定义属性
    var statusViewModel : StatusViewModel? {
        didSet{
            //1.判断statusViewModel有没有值
            guard let statusViewModel = statusViewModel else {
                return
            }
            
            //2.头像
            priflieImageView.sd_setImageWithURL(statusViewModel.iconUrl, placeholderImage: UIImage(named: "avatar_default"))
            
            //3.认证图片
            verifiedImageView.image = statusViewModel.verifiedImage
            
            //4.昵称
            screenNameLabel.text = statusViewModel.status?.user?.screen_name
            //5.VIP图片
            vipImageView.image = statusViewModel.VipImage
            //6.创建时间
            createAtLabel.text = statusViewModel.createAtString
            //7. 来源
            sourceStrLabel.text = statusViewModel.sourceStr
            //8.正文的设置
            text_label.text = statusViewModel.status?.text
            
           //9.设置collectView的数据
            pictureCollectView.picUrls = statusViewModel.pictureUrls
            
            //10.设置转发微博正文的数据
            if statusViewModel.status?.retweeted_status != nil {
                //10.1 设置label的文字
                if let retweetedScreenName = statusViewModel.status?.retweeted_status?.user?.screen_name{
                retweetedTextLabel.text = "@" + retweetedScreenName + ": " + (statusViewModel.status?.retweeted_status?.text ?? "")
                }else{
                retweetedTextLabel.text = statusViewModel.status?.retweeted_status?.text ?? ""
                }
                
                //10.2设置文字label的约束
                reweetTextLabelTopCons.constant = 15
                //10.3 设置文字label的显示
                retweetedTextLabel.hidden = false
                
            }else {
                retweetedTextLabel.text = nil
                //10.2设置文字label的约束
                reweetTextLabelTopCons.constant = 0
                //10.3 设置文字label的显示
                retweetedTextLabel.hidden = true
            }
            //10.判断是否有图片
            picViewTopCons.constant =  statusViewModel.pictureUrls.count != 0 ? 10 : 0
            
        }
    }
    
    
    override func awakeFromNib() {
        super.awakeFromNib()
        //设置正文label的最大宽度
        textLabelWidthCons.constant = UIScreen.mainScreen().bounds.width - margin * 2
        retweetedTextLabelWidthCons.constant = UIScreen.mainScreen().bounds.width - margin * 2
        
    }
    
    override func setSelected(selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}
// MARK: - 计算cell的高度
extension HomeTableViewCell {
  
    
    //计算cell的高度返回
    func cellHeight(statusViewModel :StatusViewModel) -> CGFloat {
        //给模型赋值
        self.statusViewModel = statusViewModel
        //刷新一次布局
        self.layoutIfNeeded()
        //返回cell的高度
        let toolBarBottomY = CGRectGetMaxY(toolBar.frame)
        return toolBarBottomY
    }

}
